# coding:utf-8
import yaml
import os
import json

PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))  # 项目路径
TESTDATA_PATH = os.path.join(PROJECT_PATH, "test_data\\")  # 测试数据目录
EXCEL_PATH = os.path.join(PROJECT_PATH, "test_data\\excel_data\\")  # 测试数据-excel目录
PICTURE_PATH = os.path.join(PROJECT_PATH, "test_data\\picture_data\\")  # 测试数据-图片目录
LEAGUER_HEAD_PATH = os.path.join(PROJECT_PATH, "test_data\\leaguer_picture\\")  # 测试数据-会员图片目录
REPORT_PATH = os.path.join(PROJECT_PATH, "test_report\\report\\")  # 测试报告目录
TESTCASE_PATH = os.path.join(PROJECT_PATH, "test_case\\")  # 测试用例目录
LOG_PATH = os.path.join(PROJECT_PATH, "test_report\\logs\\")  # 日志目录
CONFIG_PATH = os.path.join(PROJECT_PATH, "configs\\")  # 配置文件目录
TOKEN_PATH = os.path.join(CONFIG_PATH, "token.json")  # token文件目录


def get_yaml_config(file_name='configs.yaml'):
    configpath = os.path.join(PROJECT_PATH, "configs")
    yamlPath = os.path.join(configpath, file_name)  # configName.yaml文件
    f = open(yamlPath, 'r', encoding='UTF-8')
    cfg = f.read()  # 读出来是字符串
    dic = yaml.load(cfg, Loader=yaml.FullLoader)  # 用load方法转字典
    return dic


def get_yaml_page_loc(page_name):
    """
    获取配置文件实例
    :param page_name: 配置文件名称
    :return: 字典
    """
    yamlPath = os.path.join(CONFIG_PATH, page_name)  # 获取config元素定位目录
    f = open(yamlPath, 'r', encoding='UTF-8', errors='ignore')
    dic = yaml.load(f, Loader=yaml.FullLoader)  # 用load方法转字典
    return dic


def write_token(token):
    with open(TOKEN_PATH, 'w') as fp:
        fp.write(json.dumps(token))


def read_token():
    with open(TOKEN_PATH) as fp:
        _data = json.load(fp)
        return _data


def assign_write_token(token, filename):
    """
    指定文件名写入token
    :param token: 单点response的json串
    :param filename: 指定存放token的文件名
    :return:
    """
    with open(os.path.join(CONFIG_PATH, filename), 'w') as fp:
        fp.write(json.dumps(token))


def assign_read_token(filename):
    """
    指定文件名读取token
    :param filename: token文件名，默认存放在CONFIG_PATH文件夹下
    :return: 单点登录response的json串
    """
    with open(os.path.join(CONFIG_PATH, filename)) as fp:
        _data = json.load(fp)
        return _data



if __name__ == "__main__":
    loginpage = get_yaml_page_loc('configs.yaml')
    data = {"token": "test"}
    write_token(token=data)
    print(type(read_token()))
